package cn.yoyo.controller.fore;

import cn.yoyo.entity.Book;
import cn.yoyo.tools.RespJson;
import cn.yoyo.service.BookService;
import cn.yoyo.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PublicController {
    @Autowired
    BookService bookService;
    @Autowired
    BookTypeService bookTypeService;

    @RequestMapping("/api/fore/info/getUser")
    public Object findUserByAccount() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println("principal=" + principal);
        if (principal == null) return null;
        if (principal instanceof UserDetails) {
            return (UserDetails) principal;
        } else {
            return String.valueOf(principal);
        }
    }

    @RequestMapping("/api/public/book/findIf/{start}/{num}")
    public Map<String, Object> findIf(@PathVariable("start") Integer start,
                                      @PathVariable("num") Integer num, String keyWord) {
        System.out.println(start);
        System.out.println(num);
        System.out.println(keyWord);
        if (keyWord == null && "".equals(keyWord)) return RespJson.pack(404, "查询条件为空", null);
        Map<String, Object> value = new HashMap<>();
        value.put("keyWord", keyWord);
        List<Book> list = bookService.findIf(start, num, keyWord);
        for (Book book : list) {
            String name = book.getName();
            String author = book.getAuthor();
            String publisher = book.getPublisher();
            if (name.indexOf(keyWord) != -1) {
                name = name.replace(keyWord, "<span style='color:orange;'>" + keyWord + "</span>");
                book.setName(name);
            }
            if (author.indexOf(keyWord) != -1) {
                author = author.replace(keyWord, "<span style='color:orange;'>" + keyWord + "</span>");
                book.setAuthor(author);
            }
            if (publisher.indexOf(keyWord) != -1) {
                publisher = publisher.replace(keyWord, "<span style='color:orange;'>" + keyWord + "</span>");
                book.setPublisher(publisher);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", bookService.totalPage(value));
        map.put("books", list);
        return RespJson.pack(200, "", map);
    }

    @RequestMapping("/api/public/getTotalPage")
    public Map<String, Object> getTotalPage() {
        return RespJson.pack(200, "", bookService.totalPage(new HashMap<>()));
    }

    @RequestMapping("/api/public/type/findAll")
    public Map<String, Object> findAll() {
        return RespJson.pack(200, "", bookTypeService.findAll());
    }

    @RequestMapping("/api/public/findBookByPage/{start}/{num}/{typeid}")
    public Map<String, Object> findBookByPage(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid) {
        Map<String, Object> value = new HashMap<>();
        value.put("typeid", typeid);
        Map<String, Object> map = new HashMap<>();
        map.put("total", bookService.totalPage(value));
        map.put("books", bookService.findBookByPage(start, num, typeid));
        return RespJson.pack(200, "", map);
    }

    @RequestMapping("/api/public/showImg/{id}")
    public void showImg(HttpServletResponse response, @PathVariable("id") String id) {
        response.setContentType("image/jpeg");
        try {
            byte[] bytes = (byte[]) bookService.showImg(id);
            OutputStream os = response.getOutputStream();
            os.write(bytes);
            os.flush();
            os.close();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }


    @RequestMapping("/api/public/findBookByPageRate/{start}/{num}/{typeid}")
    public Map<String, Object> findBookByPageRate(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid) {
        Map<String, Object> value = new HashMap<>();
        value.put("typeid", typeid);
        Map<String, Object> map = new HashMap<>();
        map.put("total", bookService.totalPage(value));
        map.put("books", bookService.findBookByPageRate(start, num, typeid));
        return RespJson.pack(200, "", map);
    }


    @RequestMapping("/api/public/findBookByPageSale/{start}/{num}/{typeid}")
    public Map<String, Object> findBookByPageSale(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid) {
        Map<String, Object> value = new HashMap<>();
        value.put("typeid", typeid);
        Map<String, Object> map = new HashMap<>();
        map.put("total", bookService.totalPage(value));
        map.put("books", bookService.findBookByPageSale(start, num, typeid));
        return RespJson.pack(200, "", map);
    }


    @RequestMapping("/api/public/findBookByPageFav/{start}/{num}/{typeid}")
    public Map<String, Object> findBookByPageFav(@PathVariable("start") Integer start, @PathVariable("num") Integer num, @PathVariable("typeid") Integer typeid) {
        Map<String, Object> value = new HashMap<>();
        value.put("typeid", typeid);
        Map<String, Object> map = new HashMap<>();
        map.put("total", bookService.totalPage(value));
        map.put("books", bookService.findBookByPageFav(start, num, typeid));
        return RespJson.pack(200, "", map);
    }
}
